package com.lhhu.demo.controller;

import com.lhhu.demo.bean.HandlersMessage;
import com.lhhu.demo.bean.ReturnMessage;
import com.lhhu.demo.config.LogAnnotation;
import com.lhhu.demo.service.HandlersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Auther 虎
 * @Data 2021/3/7 17:55
 * @Version 1.0
 */
@RestController
public class HandlersController {
    @Autowired
    private HandlersService handlersService;

    @LogAnnotation(module = "登录模块",action = "修改密码")
    @PutMapping("/updateHandlerPassword")
    public int updateHandlerPassword(String codes, String passwords1, String passwords){
        return handlersService.updateHandlerPassword(codes,passwords1,passwords);
    }

    @LogAnnotation(module = "登录模块",action = "登录系统")
    @GetMapping("/getOneHandlersMessage")
    public ReturnMessage getOneHandlersMessage(@DefaultValue("110") String codes,String username,String password){
        try {
            HandlersMessage handlersMessage = handlersService.getOneHandlersMessage(username);
            ReturnMessage returnMessage = new ReturnMessage();
            String code = UUID.randomUUID().toString();
            if(handlersMessage !=null){
                if(handlersMessage.getPasswords().equals(password)){
                    handlersMessage.setCodes(code);
                    //写入codes
                    handlersService.updateHandlerCodes(username,code);
                    returnMessage.setCode(200);
                    returnMessage.setMessage(code);
                }else{
                    returnMessage.setCode(401);
                    returnMessage.setMessage("密码错误！");
                }
            }else {
                returnMessage.setCode(404);
                returnMessage.setMessage("账号不存在！");
            }
            return returnMessage;
        }catch (Exception exception){
            ReturnMessage returnMessage = new ReturnMessage();
            returnMessage.setCode(500);
            returnMessage.setMessage("服务器出错！");
            return returnMessage;
        }
    }

    @LogAnnotation(module = "登录模块",action = "退出系统")
    @PostMapping("/exit")
    public ReturnMessage exit(String codes,String actions){
        ReturnMessage returnMessage = new ReturnMessage();
        returnMessage.setCode(200);
        returnMessage.setMessage("退出成功！");
        return returnMessage;
    }
}
